package com.ireadbooks.service.listener;

import com.ireadbooks.entity.OrderLog;
import com.ireadbooks.service.OrderLogService;
import com.ireadbooks.service.event.OrderStatusEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: spring-event-threadpool
 * @description: 日志监听器
 * @author: wutao
 * @create: 2020/04/04 17:52
 **/
@Service
public class OrderStatusLogListener implements ApplicationListener<OrderStatusEvent> {
    private static final Logger logger = LoggerFactory.getLogger(OrderStatusLogListener.class);

    @Autowired
    private OrderLogService orderLogService;

    // 对接收到的事件进行处理
    @Override
    public void onApplicationEvent(OrderStatusEvent event) {
        logger.info("OrderStatusLogListener 监听到消息：{}", event);
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(event.getOrderId());
        orderLog.setFromStatus(event.getFromStatus());
        orderLog.setToStatus(event.getToStatus());
        orderLog.setCreateDate(new Date());
        orderLogService.insert(orderLog);
    }
}
